package fr.esiag.isidis.pdsTeam.swing;

import java.awt.Color;
import java.awt.FlowLayout;
import java.lang.reflect.Field;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Form extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1118134102042758177L;
	private int labelWidth = 100;
	private JPanel content;
	
	public Form() {
		setLayout(new FlowLayout(FlowLayout.LEFT));
		content = new JPanel();
		this.add(content);
		
		content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
		setBorder(BorderFactory.createLineBorder(Color.blue));//Bordure Form
		content.setBorder(BorderFactory.createLineBorder(Color.green));//Bordure content
	}
	
	public Form(int labelWidth) {
		this();
		this.labelWidth = labelWidth;
	}
	
	@SuppressWarnings("rawtypes")
	public Form(Class c) {
		this();
		Field t[] = c.getDeclaredFields();
		for (int i = 0; i < t.length; i++) {
			//System.out.println(t[i].getName());
			addField(t[i].getName(), 20);
		}
	}
	
	public Form(Item item) {
		this();
		for (int i = 0; i < item.getFieldsCount(); i++) {
			addField(item.getField(i), item.getFieldSize(i));
		}
	}
	
	public Form(Item item, int labelWidth) {
		this();
		this.labelWidth = labelWidth;
		for (int i = 0; i < item.getFieldsCount(); i++) {
			addField(item.getField(i), item.getFieldSize(i));
		}
	}
	public int getLabelWidth() {
		return labelWidth;
	}

	public void setLabelWidth(int labelWidth) {
		this.labelWidth = labelWidth;
	}

	public void addField(String label, int size) {
		LabeledTextField t1 = new LabeledTextField(label, size, labelWidth);
		content.add(t1);
		
	}
	public String getField(int i)
	{
		return ((JTextField)((LabeledTextField)content.getComponent(i)).getComponent(1)).getText();
	}
}
